<?php 
	namespace Home\Model;
	use Think\Model;

	class PermissionModel extends Model
	{
		protected $trueTableName = 't_permission';
 		
 		public function get_permisses($ids=null,$status=false,$returnNodes=false)
    	{
    		// 角色id对应相应权限
    		// dump($ids);exit;
	        $menus = array();
	        $nodes = array();
	        $map = array();
	        $status && $map["status"] = 1;
	        ($ids!=[]) && $map["id"] = array("in",$ids);

	        $m = $this->where($map)->select();
	        if($ids == []){
	        	$m = [];
	        }
	        // dump($m);exit;
	        $list = array();
	        $sub = array();
	        $sons = array();
	        foreach($m as $v)
	        {
	            $tmp = array(
	                "id"=>$v["id"],
	                "permission_name"=>strval($v["permission_name"]),
	                "type"=>strval($v["type"]),
	                "url"=>strval($v["url"]),
	                "pid"=>intval($v["parent_id"]),
	                "status"=>$v["status"],
	            );
	            if($tmp["pid"])
	            {
	                $sub[$tmp["pid"]][] = $tmp;
	                $sons[$tmp["pid"]][] = $tmp["id"];
	            }else{
	                $list[] = $tmp;
	            }
	        }
	        // dump("111");dump($list);exit;
	        foreach ($list as $v)
	        {
	            if($status)
	            {
	                $sub[$v["id"]] && $v["sub"] = $sub[$v["id"]];
	                $menus[] = $v;
	            }else{
	                $menus[] = $v;
	                if($sub[$v["id"]])
	                {
	                    foreach($sub[$v["id"]] as $vv)
	                    {
	                        $menus[] = $vv;
	                    }
	                }
	            }
	        }
	        // dump($menus);exit;
	        if($returnNodes)
	        {
	            foreach($m as $v)
	            {
	                $nodes[$v["id"]] = array(
	                    "p"=>intval($v["parent_id"]),
	                    "s"=>$sons[$v["id"]]?$sons[$v["id"]]:array()
	                );
	            }
	            return array("menus"=>$menus,"nodes"=>$nodes);
	        }else{
	            return $menus;
	        }
	    }

	    public function get_all($page,$rows,$type)
	    {
	    	if(isset($type)){
	    		$res = $this->where(array("type"=>0))->select();
	    	}
	    	$total = count($this->select());
	    	if($page && $rows){
	    		$res = $this->page($page,$rows)->select();
	    	}else{
	    		$res = $this->select();
	    	}
	    	$s['res'] = $res; 
	    	$s['total'] = $total; 
	    	return $s?$s:'';
	    }

	    public function add_per($permission_name,$type,$url,$status,$parent_id)
	    {
	    	$data['permission_name'] = $permission_name;
	    	$data['type'] = $type;
	    	$data['url'] = $url;
	    	$data['status'] = $status;
	    	$data['parent_id'] = $parent_id;
	    	$res = $this->add($data);
	    	return $res?true:false;
	    }

	    public function delete_per($id)
	    {
	    	$res = $this->where(array("id"=>$id))->delete();
	    	return $res?true:false;
	    }

	    public function select_per($id)
	    {
			$res = $this->where(array("id"=>$id))->select();
			return $res?$res:'';
	    }

	    public function edit_per($id,$permission_name,$type,$url,$status,$parent_id)
	    {
	    	$data['permission_name'] = $permission_name;
	    	$data['type'] = $type;
	    	$data['url'] = $url;
	    	$data['status'] = $status;
	    	$data['parent_id'] = $parent_id;
			$res = $this->where(array("id"=>$id))->save($data);
			return $res?true:false;
	    }

	    public function get_perm_id($last_id,$auth)
	    {
	    	$auth = explode(',', $auth);
	    	foreach ($auth as $key => $value) {
	    		$ids[] = $this->where(array("permission_name"=>$value))->getField("id");
	    	}
	    	return D("RolePermission")->add_role($last_id,$ids);
	    }

	    public function get_perm_id1($id,$auth)
	    {
	    	// dump($auth);exit();
	    	$auth = explode(',', $auth);
	    	foreach ($auth as $key => $value) {
	    		$ids[] = $this->where(array("permission_name"=>$value))->getField("id");
	    	}
	    	return D("RolePermission")->edit_role($id,$ids);
	    }
	}
 ?>